import { useState, useEffect } from 'react';
import { invoke } from '@tauri-apps/api/core';

interface VideoStats {
  totalProcessed: number;
  totalSavedTime: number; // in minutes
  totalSavedSpace: number; // in MB
  successRate: number;
  averageProcessingTime: number; // in minutes
  todayProcessed: number;
  weeklyTrend: number;
  popularFormats: Array<{
    format: string;
    count: number;
    percentage: number;
  }>;
}

export const useVideoStats = () => {
  const [stats, setStats] = useState<VideoStats>({
    totalProcessed: 0,
    totalSavedTime: 0,
    totalSavedSpace: 0,
    successRate: 0,
    averageProcessingTime: 0,
    todayProcessed: 0,
    weeklyTrend: 0,
    popularFormats: [],
  });
  const [isLoading, setIsLoading] = useState(true);
  const [error, setError] = useState<string | null>(null);

  useEffect(() => {
    const fetchStats = async () => {
      try {
        // 模拟视频统计信息获取
        const mockStats: VideoStats = {
          totalProcessed: 1247,
          totalSavedTime: 4126, // 68.7 hours
          totalSavedSpace: 2300, // 2.3 GB
          successRate: 95.3,
          averageProcessingTime: 3.2,
          todayProcessed: 12,
          weeklyTrend: 23,
          popularFormats: [
            { format: 'MP4', count: 567, percentage: 45.5 },
            { format: 'AVI', count: 234, percentage: 18.7 },
            { format: 'MOV', count: 198, percentage: 15.9 },
            { format: 'MKV', count: 156, percentage: 12.5 },
            { format: '其他', count: 92, percentage: 7.4 },
          ],
        };

        setStats(mockStats);
        setIsLoading(false);
      } catch (err) {
        setError('获取视频统计信息失败');
        setIsLoading(false);
      }
    };

    fetchStats();
  }, []);

  const refreshStats = async () => {
    setIsLoading(true);
    setError(null);
    
    try {
      // 模拟刷新统计数据
      const updatedStats: VideoStats = {
        totalProcessed: stats.totalProcessed + Math.floor(Math.random() * 10),
        totalSavedTime: stats.totalSavedTime + Math.floor(Math.random() * 30),
        totalSavedSpace: stats.totalSavedSpace + Math.floor(Math.random() * 50),
        successRate: Math.max(90, Math.min(98, stats.successRate + (Math.random() - 0.5) * 2)),
        averageProcessingTime: Math.max(2, Math.min(5, stats.averageProcessingTime + (Math.random() - 0.5) * 0.5)),
        todayProcessed: Math.floor(Math.random() * 5) + 8,
        weeklyTrend: stats.weeklyTrend + (Math.random() - 0.5) * 10,
        popularFormats: stats.popularFormats.map(format => ({
          ...format,
          count: format.count + Math.floor(Math.random() * 3),
        })),
      };

      // 重新计算百分比
      const totalCount = updatedStats.popularFormats.reduce((sum, format) => sum + format.count, 0);
      updatedStats.popularFormats = updatedStats.popularFormats.map(format => ({
        ...format,
        percentage: Math.round((format.count / totalCount) * 1000) / 10,
      }));

      setStats(updatedStats);
    } catch (err) {
      setError('刷新统计信息失败');
    } finally {
      setIsLoading(false);
    }
  };

  return {
    stats,
    isLoading,
    error,
    refreshStats,
  };
};